#include<bits/stdc++.h>
#define N 2500
#define int long long
using namespace std;
int n, ans = -1e9;
int a[N], f[N], now[N], vis[20], b[20];
vector<int>p[N];
void dfs(int k) {
    //cout << ans << endl;
    if(k > n) {
        memset(vis, 0, sizeof(vis));
        for(int i = 1; i <= n; i++) b[i] = a[i];
        int num = 0;
        for(int I = 1; I <= n; I++) {
            int i = now[I];
            vis[i] = 1;
            num += b[i];
            for(int j = 0; j < p[i].size(); j++) {
                int k = p[i][j];
                if(!vis[k]) {
                    b[k] += b[i];
				}
			}
		}
        ans = max(ans, num);
        return;
	}
	for(int i = 1; i <= n; i++)
	    if(!f[i]) {
	        f[i] = 1;
	        now[k] = i;
	        dfs(k + 1);
	        f[i] = 0;
		}
}
signed main() {
	freopen("light.in", "r", stdin);
    freopen("light.out", "w", stdout);
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> a[i];
    for(int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        p[u].push_back(v);
        p[v].push_back(u);
	}

    dfs(1);
    cout << ans;
	
	
	return 0;
}
